package com.cmict.data.infra.service.consult;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.cmict.data.infra.controller.admin.consult.vo.*;
import com.cmict.data.infra.dal.dataobject.consult.ConsultDO;
import com.cmict.data.framework.common.pojo.PageResult;

import com.cmict.data.infra.convert.consult.ConsultConvert;
import com.cmict.data.infra.dal.mysql.consult.ConsultMapper;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.infra.enums.ErrorCodeConstants.*;

/**
 * 咨询管理 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ConsultServiceImpl implements ConsultService {

    @Resource
    private ConsultMapper consultMapper;

    @Override
    public Long createConsult(ConsultCreateReqVO createReqVO) {
        // 插入
        ConsultDO consult = ConsultConvert.INSTANCE.convert(createReqVO);
        consultMapper.insert(consult);
        // 返回
        return consult.getId();
    }

    @Override
    public void updateConsult(ConsultUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateConsultExists(updateReqVO.getId());
        // 更新
        ConsultDO updateObj = ConsultConvert.INSTANCE.convert(updateReqVO);
        consultMapper.updateById(updateObj);
    }

    @Override
    public void deleteConsult(Long id) {
        // 校验存在
        this.validateConsultExists(id);
        // 删除
        consultMapper.deleteById(id);
    }

    private void validateConsultExists(Long id) {
        if (consultMapper.selectById(id) == null) {
            throw exception(CONSULT_NOT_EXISTS);
        }
    }

    @Override
    public ConsultDO getConsult(Long id) {
        return consultMapper.selectById(id);
    }

    @Override
    public List<ConsultDO> getConsultList(Collection<Long> ids) {
        return consultMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ConsultDO> getConsultPage(ConsultPageReqVO pageReqVO) {
        return consultMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ConsultDO> getConsultList(ConsultExportReqVO exportReqVO) {
        return consultMapper.selectList(exportReqVO);
    }

    @Override
    public void batchDelete(Collection<Long> ids) {
         consultMapper.deleteBatchIds(ids);
    }

    @Override
    public List<ConsultDO> selectListChoose(String issueTitle,Long id) {
        return consultMapper.selectListChoose(issueTitle,id);
    }

}
